package com.gitee.wsl.android.ui.sample.icon


import android.animation.ValueAnimator
import android.graphics.*
import android.view.animation.AccelerateDecelerateInterpolator


/**
 * Created by codeest on 16/11/6.
 * 最简单的一个，先拿这个热热身~
 */
class ENRefreshView : AnimatedIcon() {
    private var mPaint: Paint? = null
    private var mWidth = 0
    private var mHeight = 0
    private var mCircleRadius = 0
    private var mArrowLength = 0f
    private var mArrowVarLength = 0f
    private var mArcVarLength = 0f
    private var mCenterX = 0
    private var mCenterY = 0
    private var mArrowX = 0f
    private var mArrowY = 0f
    private var mFraction = 0f
    private var mDuration = 0

    /*constructor(context: Context?) : super(context) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.refresh)
        val color = ta.getColor(R.styleable.refresh_refresh_line_color, DEFAULT_LINE_COLOR)
        val width = ta.getInteger(R.styleable.refresh_refresh_line_width, DEFAULT_LINE_WIDTH)
        ta.recycle()

    }*/

    init {
        mDuration = DEFAULT_DURATION
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.strokeCap = Paint.Cap.ROUND
        mPaint!!.color = DEFAULT_LINE_COLOR
        mPaint!!.strokeWidth = DEFAULT_LINE_WIDTH.toFloat()
    }

     fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        mWidth = w
        mHeight = h
        mCenterX = mWidth / 2
        mCenterY = mHeight / 2
        mCircleRadius = mCenterX / 3 * 2
        mArrowLength = (mWidth / 6).toFloat()
        mArrowX = (-mCircleRadius * Math.sin(30 * Math.PI / 180)).toFloat()
        mArrowY = (-mCircleRadius * Math.cos(30 * Math.PI / 180)).toFloat()
    }

    override fun draw(canvas: Canvas) {
        if (mFraction <= 0.2) {                     //转上半圈
            mArrowVarLength = mArrowLength / 0.4f * (0.4f - mFraction)
            mArcVarLength = 119.99f / 0.2f * mFraction
        } else if (mFraction <= 0.4) {              //转下半圈
            mArrowVarLength = mArrowLength / 0.4f * (0.4f - mFraction)
            mArcVarLength = 119.99f / 0.2f * (0.4f - mFraction)
        } else if (mFraction <= 0.55) {             //再弹.
            mArrowVarLength = mArrowLength * 1.4f / 0.15f * (mFraction - 0.4f)
            mArcVarLength = -28 / 0.15f * (mFraction - 0.4f)
        } else if (mFraction <= 0.7) {              //再弹..
            mArrowVarLength =
                mArrowLength * 1.4f - mArrowLength * 0.7f / 0.15f * (mFraction - 0.55f)
            mArcVarLength = -28 + 36 / 0.15f * (mFraction - 0.55f)
        } else if (mFraction <= 0.85) {             //再弹...
            mArrowVarLength = mArrowLength * 0.7f + mArrowLength * 0.5f / 0.15f * (mFraction - 0.7f)
            mArcVarLength = 14 - 20 / 0.15f * (mFraction - 0.7f)
        } else {                                    //好吧...再弹 =。=
            mArrowVarLength =
                mArrowLength * 1.2f - mArrowLength * 0.2f / 0.15f * (mFraction - 0.85f)
            mArcVarLength = -6 + 6 / 0.15f * (mFraction - 0.85f)
        }
        canvas.save()
        canvas.translate(mCenterX.toFloat(), mCenterY.toFloat())
        if (mFraction <= 0.4) {
            canvas.rotate((-10 + 360 / 0.4 * mFraction).toFloat())
        } else {
            canvas.rotate(-10f)
        }
        canvas.drawArc(
            -mCircleRadius.toFloat(),
            -mCircleRadius.toFloat(),
            mCircleRadius.toFloat(),
            mCircleRadius.toFloat(),
            0 - mArcVarLength,
            240 + mArcVarLength,
            false,
            mPaint!!
        )
        canvas.drawLine(mArrowX, mArrowY, mArrowX - mArrowVarLength, mArrowY, mPaint!!)
        canvas.drawLine(mArrowX, mArrowY, mArrowX, mArrowY + mArrowVarLength, mPaint!!)
        canvas.restore()
    }

    fun startRefresh() {
        mArrowLength = (mWidth / 6).toFloat()
        val valueAnimator = ValueAnimator.ofFloat(1f, 100f)
        valueAnimator.duration = mDuration.toLong()
        valueAnimator.interpolator = AccelerateDecelerateInterpolator()
        valueAnimator.addUpdateListener { valueAnimator ->
            mFraction = valueAnimator.animatedFraction
            invalidateSelf()
        }
        if (!valueAnimator.isRunning) {
            valueAnimator.start()
        }
    }

     fun setDuration(duration: Int) {
        mDuration = duration
    }

    override fun startAnimation() {
        startRefresh()
    }

     fun pause() {
        TODO("Not yet implemented")
    }

    companion object {
        private const val DEFAULT_LINE_COLOR = Color.WHITE
        private const val DEFAULT_LINE_WIDTH = 14
        private const val DEFAULT_DURATION = 2000
    }
}